package com.uchedao.vehicledetection.view;

import android.content.ContentValues;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;

import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.adapter.DetectionAdapter;
import com.uchedao.vehicledetection.adapter.DetectionAdapter.DetectionSingleAdapterListener;
import com.uchedao.vehicledetection.db.DbHelper;
import com.uchedao.vehicledetection.global.GlobalInfo;
import com.uchedao.vehicledetection.loadstate.UCheDaoRelativeLayout;
import com.uchedao.vehicledetection.model.Detection;
import com.uchedao.vehicledetection.model.Draft;
import com.uchedao.vehicledetection.model.ResultBoolean;
import com.uchedao.vehicledetection.view.widget.stickylistheaders.StickyListHeadersListView;

import java.util.ArrayList;

;

/**
 * Description:
 * Date: 2015-09-15  15:14
 * User: wushan
 */
public class FireView extends UCheDaoRelativeLayout implements DetectionSingleAdapterListener{


    private DetectionAdapter adapter;

    private ResultBoolean[] fire ;//cloneFire(GlobalInfo.draft.fire);

    public FireView(Context context) {
        super(context);
    }

    public FireView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FireView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    /**
     * 克隆Photo数组
     *
     * @param array
     * @return
     */
    private ResultBoolean[] cloneFire(ResultBoolean[] array) {
        ResultBoolean[] fire = new ResultBoolean[array.length];
        for (int i = 0; i < array.length; i++)
            if (array[i] != null)
                fire[i] = array[i].clone();
        return fire;
    }


    @Override
    public boolean save() {

        ContentValues values = new ContentValues();
        values.put(DbHelper.KEY_DRAFT_FIRE, toJson(fire));

        int status = GlobalInfo.draft.status[Draft.TYPE_FIRE];
        // 若原先状态为未完成，且数据已变动，则状态改变为已完成
        if (GlobalInfo.draft.status[Draft.TYPE_FIRE] != Draft.STATUE_FINISH) {
            values.put(DbHelper.KEY_DRAFT_FIRE_STATUS, Draft.STATUE_FINISH);
            status = Draft.STATUE_FINISH;
        }

        // 保存成功
        if (GlobalInfo.dbHelper.updateDraf(GlobalInfo.draft.car_plate, values)) {
            GlobalInfo.draft.fire = fire.clone();
            GlobalInfo.draft.status[Draft.TYPE_FIRE] = status;
            return true;
        }

        return false;
    }

    @Override
    public boolean dataChanged() {

        // 若未开始，则默认认为值已更改
           if (GlobalInfo.draft.status[Draft.TYPE_FIRE] == Draft.STATUE_NONE)
                return true;

        return !toJson(fire).equals(toJson(GlobalInfo.draft.fire));
//        return false;
    }

    @Override
    public void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.view_detection, this);
        StickyListHeadersListView stickListHeadersListView = (StickyListHeadersListView)
                findViewById(R.id.detection_listView);
        adapter = new DetectionAdapter(mContext, getListData(), this);
        stickListHeadersListView.setAdapter(adapter);

    }

    @Override
    public void initDataAfterView() {

    }

    @Override
    public void initListener() {

    }

    private ArrayList<Detection> getListData() {

        ArrayList<Detection> array = new ArrayList<Detection>();

        String[] headers = GlobalInfo.resources.getStringArray(R.array.fire_tabspec_entries);

        int[] arrayId = { R.array.frie_line_entries, R.array.frie_sign_entries, R.array.fire_insurance_check_entries,
                R.array.fire_bluing_values };

      fire  = cloneFire(GlobalInfo.draft.fire);

        Detection detection = null;

        for (int i = 0, k = 0; i < headers.length; i++) {
            String[] texts = GlobalInfo.resources.getStringArray(arrayId[i]);
            for (String text : texts) {
                if (fire[k] == null) {
                    fire[k] = new ResultBoolean();
                    fire[k].name = text;
                }
                detection = new Detection(Detection.TYPE_TWO_SELECT);
                detection.setHeader(headers[i]);
                detection.setText(text);
                detection.setRequestFocus(true);
                detection.setResId(R.array.boolean_entries);
                detection.setValue(fire[k++].result ? 0 : 1);
                array.add(detection);
            }
        }
        return array;
    }

    @Override
    public void onSingleResult(int position, int value) {
        fire[position].result = value == 0;
    }
}
